import pymysql

con = pymysql.connect(host="localhost",user="root",password="123456",database="spider")
# 获取游标
cursor = con.cursor()
# 新增语句
insert_sql = "insert into users values(NULL,'李四',19)"
# 执行sql
cursor.execute(insert_sql)
# 提交操作
con.commit()
# 将名单中的数据 插入数据库
strs = ""
with open("名单.txt","r+",encoding="utf-8") as f:
    strs = f.read()
print(strs)
data = strs.split("\n")
print(data)
data = [i.split(",") for i in data]
print(data)
# 使用站位符的方式
insert_sql = "insert into users values(NULL,%s,%s)"
for d in data:
    cursor.execute(insert_sql,d)
con.commit()
